package my.soft.aiteacher.teacher.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import my.soft.aiteacher.exception.BusinessException;
import my.soft.aiteacher.teacher.pojo.ConceptExampleDo;
import my.soft.aiteacher.teacher.mapper.ConceptExampleMapper;
import my.soft.aiteacher.teacher.pojo.ConceptExampleVo;
import my.soft.aiteacher.teacher.pojo.conceptExampleType.ConceptExample;
import my.soft.aiteacher.teacher.service.IConceptExampleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 概念解析 服务实现类
 * </p>
 *
 * @author GMCX
 * @since 2025-06-05
 */
@Service
public class ConceptExampleServiceImpl extends ServiceImpl<ConceptExampleMapper, ConceptExampleDo> implements IConceptExampleService {
    @Resource
    private ConceptExampleMapper conceptExampleMapper;

    @Override
    public void upload(List<ConceptExample> conceptExampleVos) {
        conceptExampleMapper.delete(new QueryWrapper<ConceptExampleDo>().in("file_id",conceptExampleVos.stream().map(ConceptExample::getFileId).collect(Collectors.toList())));

        List<ConceptExampleDo> conceptExampleVoList = new ArrayList<>();
        for(ConceptExample conceptExampleVo : conceptExampleVos){
            if(Objects.isNull(conceptExampleVo.getFileId())){
                throw new BusinessException("文件id为空");
            }
            String validationTags = String.join(",", conceptExampleVo.getInput().getValidationTags());
            ConceptExampleDo conceptExampleDo = ConceptExampleDo.builder()
                    .validationTags(validationTags)
                    .basicConcepts(conceptExampleVo.getInput().getBasicConcepts())
                    .principle(conceptExampleVo.getOutput().getPrinciple())
                    .example(conceptExampleVo.getOutput().getApplicationScenarios().getExample())
                    .contrast(conceptExampleVo.getOutput().getApplicationScenarios().getContrast())
                    .fileId(conceptExampleVo.getFileId())
                    .build();
            conceptExampleVoList.add(conceptExampleDo);
        }
        conceptExampleMapper.insert(conceptExampleVoList);
    }
}
